package gui;

import gui.clock.AbstractClock;
import gui.clock.ClockDisplay;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Hashtable;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import common.Constants;
import common.EarthSimSettings;

public class PresentationControlPanel extends JPanel {
  private static final long serialVersionUID = -5952311171081334363L;

  private EarthSimSettings settings = EarthSimSettings.getInstance();
  private JSlider displayRate;  
  private final EarthPanel pnlEarth;
  private JSlider imageOpacity;
  private final JCheckBox chkAnimate;
  private ClockDisplay clockDisplay;
  
  public PresentationControlPanel(EarthPanel pnlEarth, ClockDisplay clockDisplay) {
    super();
    this.pnlEarth = pnlEarth;
    this.clockDisplay = clockDisplay;
    settings.setPresentationPanel(this);
    
    setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
    setBorder(BorderFactory.createTitledBorder("Presentation Controls"));
    
    Dimension dim = new Dimension(Constants.FRAME_WIDTH,70);
    setPreferredSize(dim);
    setMaximumSize(dim);
    
    chkAnimate = new JCheckBox("Animate");
    chkAnimate.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        settings.setAnimation(chkAnimate.isSelected());
        setContols();
       }
     });
    
    add(chkAnimate);
    add(Box.createHorizontalStrut(30));
    add(getOpacityControl());
    add(Box.createHorizontalStrut(30));
    add(getDisplayRateControl());
    add(Box.createHorizontalStrut(20));
    add(clockDisplay);
    setContols();
  }
  
  public void setContols() {
    boolean active = chkAnimate.isSelected() && settings.isQueryRunning();
    imageOpacity.setEnabled(active);
    displayRate.setEnabled(active);
  }
  
  public void setClockType(AbstractClock clock) {
    clockDisplay.setClockType(clock);
  }
  
  private JPanel getOpacityControl() {
    imageOpacity = new JSlider(0, 100, (int)(Constants.OPACITY*100));
    imageOpacity.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent e) {
        JSlider slider = (JSlider) e.getSource();
        pnlEarth.setMapOpacity(slider.getValue() / 100f);
      };
    });
    imageOpacity.setPreferredSize(new Dimension(150, 20));
    imageOpacity.setMaximumSize(new Dimension(150, 20));
    
    JPanel pnlOpacity = new JPanel();
    pnlOpacity.setLayout(new BoxLayout(pnlOpacity, BoxLayout.LINE_AXIS));
    pnlOpacity.setAlignmentX(Component.LEFT_ALIGNMENT);
    pnlOpacity.add(new JLabel("Image opacity:"));
    pnlOpacity.add(imageOpacity);
    return pnlOpacity;        
  }
  
  private JPanel getDisplayRateControl() {
    displayRate = new JSlider(Constants.MIN_DISPLAY_RATE, 
                              Constants.MAX_DISPLAY_RATE, 
                              Constants.DEFAULT_DISPLAY_RATE);
    
    displayRate.setMajorTickSpacing(Constants.MAX_DISPLAY_RATE/2);
    displayRate.setPaintTicks(true);
    Hashtable labelTable = new Hashtable();
    labelTable.put( new Integer( Constants.MIN_DISPLAY_RATE ), new JLabel("per ms") );
    labelTable.put( new Integer( Constants.MAX_DISPLAY_RATE/2 ), new JLabel("per half sec") );
    labelTable.put( new Integer( Constants.MAX_DISPLAY_RATE ), new JLabel("per sec") );
    displayRate.setLabelTable( labelTable );
    displayRate.setPaintLabels(true);
    
    displayRate.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent e) {
        JSlider slider = (JSlider) e.getSource();
        clockDisplay.adjustDisplayRate(slider.getValue());
      };
    });
    displayRate.setPreferredSize(new Dimension(200, 50));
    displayRate.setMaximumSize(new Dimension(200, 50));
    
    JPanel pnlDisplayRate = new JPanel();
    pnlDisplayRate.setLayout(new BoxLayout(pnlDisplayRate, BoxLayout.LINE_AXIS));
    pnlDisplayRate.setAlignmentX(Component.LEFT_ALIGNMENT);
    pnlDisplayRate.add(new JLabel("Display Rate: "));
    pnlDisplayRate.add(displayRate);
    return pnlDisplayRate;
  }
}
